<template>
  <div class="container">


    <div class="add-module">
      <h1>{{ title }}</h1>
      <hr>
      <h2 class="add-title">基本信息</h2>
      <hr class="add-divider">
      <el-form class="add-form" ref="form" :model="form" :rules="rules" label-width="80px">

          <el-row>
            <el-col :span="9" :offset="2">
              <el-form-item label="模块名称" prop="moduleName">
                <el-input v-model="form.moduleName" placeholder="请输入模块名称" />
              </el-form-item>
            </el-col>
            <el-col :span="9" :offset="2">
              <el-form-item label="模块编码" prop="moduleCode">
                <el-input v-model="form.moduleCode" placeholder="请输入模块编码" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="20" :offset="2">
              <el-form-item label="主类全名" prop="mainClassName">
                <el-input v-model="form.mainClassName"  placeholder="请输入内容" />
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="20" :offset="2">
              <el-form-item label="模块描述" prop="description">
                <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="20" :offset="2">
              <el-form-item label="当前版本" prop="currentVersion">
                <el-input v-model="form.currentVersion" placeholder="请输入当前版本" />
              </el-form-item>
            </el-col>

          </el-row>
      </el-form>
    </div>


    <div class="gen-code" >
      <h2 class="gen-title">代码生成</h2>
      <hr class="gen-divider">
      <el-form class="gen-form" ref="code-form" :model="form" label-width="120px" :rules="rules">
        <el-row>
          <el-col :span="20" :offset="2">
            <el-form-item label="生成基础路径" prop="modulePath">
              <el-input v-model="form.modulePath" placeholder="请输入生成基础路径">
<!--                <template slot="append">-->
<!--                  <el-button @click="">选择路径</el-button>-->
<!--                </template>-->
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="20" :offset="2">
            <el-form-item label="代码生成模板" prop="moduleTemplate">
              <el-input v-model="form.moduleTemplate" placeholder="请输入生成模板" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <hr>

    <div style="text-align: center">
      <el-button type="primary" @click="submitForm">保存</el-button>      <el-button type="danger" @click="generateModuleCode">生成代码</el-button>      <el-button @click="goBack">关闭</el-button>
    </div>
  </div>
</template>

<script>
import { getModule, addModule, updateModule,generateModule } from "@/api/tool/module";
export default {
  name: "modifyModule",
  data() {
    return {
      // 表单参数
      form: {},

      title:'',
      showGenCode:false,
      // 表单校验
      rules: {

        moduleCode: [
          { required: true, message: "模块编码不能为空", trigger: "blur" }
        ],
        moduleName: [
          { required: true, message: "模块名称不能为空", trigger: "blur" }
        ],
        modulePath: [
          { required: true, message: "模块路径不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.form.id = this.$route.query.id
    if(this.form.id){
      getModule(this.form.id).then(response => {
        this.form = response.data;
        this.title = "修改模块管理";
        this.showGenCode =false;
      });
    }else {
      this.title = "新增模块管理"
      this.showGenCode = true;
    }
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        moduleCode: undefined,
        moduleName: undefined,
        description: undefined,
        mainClassName: undefined,
        currentVersion: undefined,
        upgradeInfo: undefined,
        status: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateDate: undefined,
        remarks: undefined
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateModule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addModule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /**
     * 返回上级页面
     */
    goBack(){
      this.$router.push({path: '/tool/module'})
    },
    /**
     * 生成模块代码
     */
    generateModuleCode(){
      generateModule({path:this.form.modulePath,moduleCode:this.form.moduleCode,moduleName:this.form.moduleName}).then(res=>{
        if(res.code == 200){
          this.goBack()
        }else {
          console.log(res)
          this.msgError("生成失败，请重试")
        }
      })
    }
  }
};
</script>

<style scoped>
.container {
  margin: 15px;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3);
  padding: 20px;
  width: 98%;
  border-radius: 10px;
}

.add-module{
  /*height: 40vh;*/
  /*background: #00afff;*/
}

.gen-code{
  /*height: 30vh;*/
  /*background: #5cb85c;*/
}
h1 {
  font-size: 20px;
  /*font-weight: bold;*/
}
.add-title {
  margin-top: 20px;
}
.add-divider {
  border: none;
  height: 1px;
  background-color: #ccc;
  margin-top: 10px;
  margin-bottom: 20px;
  margin-left: 20px;
  margin-right: 20px;
}
.add-form {
  margin-top: 10px;
}

h2 {
  font-size: 14px;
  /*font-weight: bold;*/
  color: blue;
  font-style:italic;
  margin-left: 20px;
  margin-top: 30px;
}
.gen-title {
  margin-top: 20px;
}
.gen-divider {
  border: none;
  height: 1px;
  background-color: #ccc;
  margin-top: 10px;
  margin-bottom: 20px;
  margin-left: 20px;
  margin-right: 20px;
}
.gen-form {
  margin-top: 10px;
}

</style>
